package com.example.git_app_yanshi;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.git_app_yanshi.R;

import java.util.ArrayList;
import java.util.List;

public class NotesListActivity extends AppCompatActivity {
    private ArrayAdapter<String> adapter;
    private List<String> titles;
    private ListView listView;
    private NoteDatabaseHelper dbHelper;

    private static final int REQUEST_CODE_EDIT_NOTE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes_list);

        dbHelper = new NoteDatabaseHelper(this);

        Button addButton = findViewById(R.id.add_button);
        addButton.setOnClickListener(v -> {
            Intent intent = new Intent(this, NoteEditorActivity.class);
            intent.putExtra("isEditMode", false);
            startActivityForResult(intent, REQUEST_CODE_EDIT_NOTE);
        });

        // 加载笔记列表
        loadNotes();
    }

    private void loadNotes() {
        listView = findViewById(R.id.notes_list);
        titles = new ArrayList<>();

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] projection = {
                NoteDatabaseHelper.COLUMN_ID,
                NoteDatabaseHelper.COLUMN_TITLE,
                NoteDatabaseHelper.COLUMN_CONTENT
        };
        Cursor cursor = db.query(
                NoteDatabaseHelper.TABLE_NAME,
                projection,
                null,
                null,
                null,
                null,
                null
        );

        while (cursor.moveToNext()) {
            String title = cursor.getString(cursor.getColumnIndexOrThrow(NoteDatabaseHelper.COLUMN_TITLE));
            titles.add(title);
        }
        cursor.close();

        adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, titles);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(this, NoteEditorActivity.class);
            intent.putExtra("isEditMode", true);
            intent.putExtra("noteIndex", position);

            SQLiteDatabase readDb = dbHelper.getReadableDatabase();
            Cursor noteCursor = readDb.query(
                    NoteDatabaseHelper.TABLE_NAME,
                    projection,
                    null,
                    null,
                    null,
                    null,
                    null
            );
            if (noteCursor.moveToPosition(position)) {
                String title = noteCursor.getString(noteCursor.getColumnIndexOrThrow(NoteDatabaseHelper.COLUMN_TITLE));
                String content = noteCursor.getString(noteCursor.getColumnIndexOrThrow(NoteDatabaseHelper.COLUMN_CONTENT));
                intent.putExtra("title", title);
                intent.putExtra("content", content);
            }
            noteCursor.close();

            startActivityForResult(intent, REQUEST_CODE_EDIT_NOTE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_EDIT_NOTE && resultCode == RESULT_OK) {
            // 刷新笔记列表
            loadNotes();
        }
    }
}